/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serialize;

import diagram.Graphical;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import model.DiagramModel;

/**
 *
 * @author Jelle
 */
public class Serializer {

    private DiagramModel model;
    private String fileName = "test.txt";

    public Serializer(DiagramModel model) {
        this.model = model;
    }

    public void serialize() {
        ArrayList<Graphical> list = new ArrayList<Graphical>();
        list = model.getObjects("all");

        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try {
            fos = new FileOutputStream(fileName);
            out = new ObjectOutputStream(fos);
            out.writeObject(list);
            out.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deserialize(){
		ArrayList<Graphical> list = new ArrayList<Graphical>();
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(fileName);
			in = new ObjectInputStream(fis);
            list = (ArrayList<Graphical>)in.readObject();
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
        model.setObjects(list);

		System.out.println();
    }
}
